﻿/*=============================================================================== 
 * SQLHelper.cs
 * author:csyingwu
 * 
 * 
 * 
 */
//============================================================================

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;  
using System.Text;
using System.IO;
using System.Configuration;


namespace Kaidou.Util
{
    public sealed class DBUnit
    {
        public static string GetString(object obj)
        {
            try
            {
                if(Convert.IsDBNull(obj))
                {
                    return "";
                }
                else
                {
                    return Convert.ToString(obj).Trim();
                }
            }
            catch
            {
                return "";
            }            
        }

        public static string GetStringNoTrim(object obj)
        {
            try
            {
                if (Convert.IsDBNull(obj))
                {
                    return "";
                }
                else
                {
                    return Convert.ToString(obj);
                }
            }
            catch
            {
                return "";
            }
        }

        public static decimal GetDecimal(object obj)
        {
            try
            {
                if(Convert.IsDBNull(obj))
                {
                    return 0;
                }
                else
                {
                    return Convert.ToDecimal(obj);
                }
            }
            catch
            {
                return 0;
            }
        }

        public static Int32 GetInt32(object obj)
        {
            try
            {
                if(Convert.IsDBNull(obj))
                {
                    return 0;
                }
                else
                {
                    return Convert.ToInt32(obj);
                }
            }
            catch
            {
                return 0;
            }
        }

        public static double GetDouble(object obj)
        {
            try
            {
                if(Convert.IsDBNull(obj))
                {
                    return 0.0;
                }
                else
                {                    
                    return Convert.ToDouble(obj);
                }
            }
            catch
            {
                return 0.0;
            }
        }

        public static DateTime GetDateTime(object obj)
        {
            try
            {
                if (Convert.IsDBNull(obj))
                {
                    return DateTime.Parse("0001/01/01 0:00:00");
                }
                else
                {
                    return Convert.ToDateTime(obj);
                }
            }
            catch
            {
                string syyyyMMdd = Convert.ToString(obj);
                if (syyyyMMdd.Length == 8)
                {
                    try
                    {
                        syyyyMMdd = syyyyMMdd.Insert(6, "/");
                        syyyyMMdd = syyyyMMdd.Insert(4, "/");
                        return DateTime.Parse(syyyyMMdd);
                    }
                    catch
                    {
                        return DateTime.Parse("0001/01/01 0:00:00");
                    }
                }
                else
                {
                    return DateTime.Parse("0001/01/01 0:00:00");
                }
            }
        }

        public static  DataTable ConvertDataReaderToDataTable(SqlDataReader reader)
        {
            DataTable objDataTable = new DataTable();

            int intFieldCount = reader.FieldCount;

            for (int intCounter = 0; intCounter <= intFieldCount - 1; intCounter++)
            {
                objDataTable.Columns.Add(reader.GetName(intCounter), reader.GetFieldType(intCounter));
            }

            //populate datatable
            objDataTable.BeginLoadData();


            //object[] objValues = new object[intFieldCount -1];

            object[] objValues = new object[intFieldCount];


            while (reader.Read())
            {
                reader.GetValues(objValues);

                objDataTable.LoadDataRow(objValues, true);
            }

            reader.Close();

            objDataTable.EndLoadData();

            return objDataTable;
        }

        /// <summary>
        /// ログ
        /// </summary>
        /// <param name="ex"></param>
        public static void WriteErrorLog(Exception ex)
        {
            string strPath = System.Configuration.ConfigurationManager.AppSettings["LogFileName"];
            if (strPath == "")
                strPath = "FunsAP.LOG";
            strPath = System.AppDomain.CurrentDomain.BaseDirectory + strPath;

            StreamWriter objStreamWriter;
            string strErrorDate = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt") + " 例外発生しました:-----------------------------------------";
            if(strPath == "")
            {
                throw new System.Exception("ログファイルパスはEmptyに指定されることは出来ません。");                
            }
       
            //ファイル存在チェック。		False:新規		True:追加モード        								
            if(File.Exists(strPath))  //追加モード
                objStreamWriter = File.AppendText(strPath);
            else
                objStreamWriter = File.CreateText(strPath);
            
            objStreamWriter.WriteLine(strErrorDate);
            objStreamWriter.WriteLine(ex.ToString());
            
            objStreamWriter.Flush();
            objStreamWriter.Close();
            objStreamWriter.Dispose();             
        }
            
        /// <summary>
        /// <!--冷凍野菜業者ID　デフォルト：9999999999 -->
        /// <add key="GyousyaBanngou" value="9999999999"/>
        /// </summary>
        public static string GetGyousyaBanngou()
        {
            string strValue = System.Configuration.ConfigurationManager.AppSettings["GyousyaBanngou"];
            if (strValue == "")
            {
                strValue = "9999999999";
            }
            return strValue;
        }



        /// <summary>
        /// <!--受注時 通常商品の場合は受付番号 デフォルト：0000000000　-->
        /// <add key="UketsukeBanngou" value="0000000000"/>
        /// <summary>
        public static string GetUketsukeBanngou()
        {
            string strValue = System.Configuration.ConfigurationManager.AppSettings["UketsukeBanngou"];
            if (strValue == "")
            {
                strValue = "0000000000";
            }
            return strValue;
        }

        /// <summary>
        /// <!--冷凍野菜業者ID　デフォルト：9999999999 -->
        /// <add key="GyousyaBanngou" value="9999999999"/>
        /// </summary>
        public static string GetExcelModelPath()
        {
            string strValue = System.Configuration.ConfigurationManager.AppSettings["ExcelModelPath"];
            if (strValue == "")
            {
                strValue = "9999999999";
            }
            return strValue;
        }

        ///<summary>
        /// <!-->Max希望納品日 デフォルト：365-->
        /// <add key="nMaxKibouDays" value="365" />
        ///<summary>
        ///

        public static int GetnMaxkibouDays()
        {
            string strValue = System.Configuration.ConfigurationManager.AppSettings["nMaxKibouDays"];
            if (strValue == "")
            {
                return 365;
            }
            try
            {
                return Convert.ToInt16(strValue);
            }
            catch
            {
                return 365;
            }
        }
        
        /// <summary>
        /// <!--受注情報出力のCSVフォールだ名 デフォルト：CSV-->
        /// <add key="CSVFolderName" value="CSV" />
        /// </summary>
        /// <returns></returns>       
        public static string GetCSVFolderName()
        {
            string strValue = System.Configuration.ConfigurationManager.AppSettings["CSVFolderName"];
            if (strValue == "")
            {
                strValue = "CSV";
            }
            return strValue;
        }

        /// <summary>
        /// 日期条件を取る
        /// </summary>
        /// <param name="strKey">キイ名</param>
        /// <param name="strFromDate">開始日</param>
        /// <param name="strToDate">終了日</param>
        /// <returns></returns>
        public static bool GetConfigFTDate(string strKey, ref string strFromDate, ref string strToDate)
        {
            try
            {
                string strValue = System.Configuration.ConfigurationManager.AppSettings[strKey];
                strValue = strValue.Trim();
                if (strValue == "")
                {
                    return false;
                }
                string sType, sFrom, sTo;
                int nFrom = 0, nTo = 0;
                int nIndex = 0;
                DateTime dt = DateTime.Now;
                DateTime dtFrom,dtTo;

                sType = strValue.Substring(strValue.Length - 1, 1).ToUpper();
                nIndex = strValue.IndexOf(',');
                sFrom = strValue.Substring(0, nIndex);
                sTo = strValue.Substring(nIndex + 1, strValue.Length - nIndex - 2);     

                if (sFrom != "*")
                    nFrom = Convert.ToInt16(sFrom);               
                
                if (sTo != "*")
                    nTo = Convert.ToInt16(sTo);  

                if (sType == "Y")
                {
                    dtFrom = dt.AddYears(nFrom);
                    dtTo = dt.AddYears(nTo);
                }
                else if (sType == "M")
                {
                    dtFrom = dt.AddMonths(nFrom);
                    dtTo = dt.AddMonths(nTo);
                }
                else if (sType == "D")
                {
                    dtFrom = dt.AddDays(nFrom);
                    dtTo = dt.AddDays(nTo);
                }
                else
                {
                    return false;
                }
                if(sFrom != "*")
                    strFromDate = dtFrom.ToString("yyyyMMdd");                
                if(sTo != "*")
                    strToDate = dtTo.ToString("yyyyMMdd");           
            }
            catch
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// <!--売上情報出力 SLOGIX出力時　CSV タイトル タグ(1:タイトルはあります、0:タイトルはありません) デフォルト：1-->
        /// <add key="SLOGIXHeadFlag" value ="1"/>
        /// </summary>
        /// <returns></returns>
        public static string GetSLOGIXHeadFlag()
        {
            string strValue = System.Configuration.ConfigurationManager.AppSettings["SLOGIXHeadFlag"];
            if (strValue == "")
            {
                strValue = "1";
            }
            return strValue;
        }

        /// <summary>
        /// <!--出荷依頼明細 csv出力時　CSV タイトル タグ(1:タイトルはあります、0:タイトルはありません) デフォルト：1-->
        /// <add key="SKCsvHeadFlag" value ="1"/>
        /// </summary>
        /// <returns> CsvHeadFlag value</returns>
        public static string GetSKCsvHeadFlag()
        {
            string strValue = System.Configuration.ConfigurationManager.AppSettings["SKCsvHeadFlag"];
            if (strValue == "")
            {
                strValue = "1";
            }
            return strValue;
           
        }

        /// <summary>
        /// 曜日を取得
        /// </summary>
        /// <param name="inDate">日付</param>
        /// /// <param name="weekFlag">取得日付の表示文字類</param>
        /// <returns>
        /// weekFlag＝1　日、月、火、水、木、金、土
        /// weekFlag＝3　日曜日、月曜日、火曜日、水曜日、木曜日、金曜日、土曜日
        /// </returns> 
        /// 
        public static string getWeekStr(DateTime inDate, int weekFlag)
        {            
            string strWeek;
            strWeek = "?";
            if (weekFlag == 1)
            {

                switch (inDate.DayOfWeek)
                {
                    case DayOfWeek.Monday:
                        strWeek = "月";
                        break;
                    case DayOfWeek.Tuesday:
                        strWeek = "火";
                        break;
                    case DayOfWeek.Wednesday:
                        strWeek = "水";
                        break;
                    case DayOfWeek.Thursday:
                        strWeek = "木";
                        break;
                    case DayOfWeek.Friday:
                        strWeek = "金";
                        break;
                    case DayOfWeek.Saturday:
                        strWeek = "土";
                        break;
                    case DayOfWeek.Sunday:
                        strWeek = "日";
                        break;

                }
            }
            if (weekFlag == 3)
            {

                switch (inDate.DayOfWeek)
                {
                    case DayOfWeek.Monday:
                        strWeek = "月曜日";
                        break;
                    case DayOfWeek.Tuesday:
                        strWeek = "火曜日";
                        break;
                    case DayOfWeek.Wednesday:
                        strWeek = "水曜日";
                        break;
                    case DayOfWeek.Thursday:
                        strWeek = "木曜日";
                        break;
                    case DayOfWeek.Friday:
                        strWeek = "金曜日";
                        break;
                    case DayOfWeek.Saturday:
                        strWeek = "土曜日";
                        break;
                    case DayOfWeek.Sunday:
                        strWeek = "日曜日";
                        break;

                }
            }
            return strWeek;
        }

        /// <summary>
        /// <!--メールについて設定を取得する-->
        /// GetMailValue(int mode)
        ///  <param name="mode">メールの項目フラグ</param>
        /// mode=1, FromMail  　送信者のメールのアドレス 例：staff@systemsig.jp
        /// mode=2, FromServer　送信者のメールのサーバ　例：mail.systemsig.jp
        /// mode=3, FromName　　送信者の表示名　　　　　例： zuo
        /// mode=4, ReplyMail 　返信の連絡先のメールアドレス　　例：　staff@systemsig.jp
        /// mode=5, ReplyName　 返信の連絡先の名　例： zzs
        /// mode=6, MailBody 　 メールの内容　例： お世話になっております。。。。。
        /// </summary>
        /// <returns>メールの項目の設定値</returns>
        public static string GetMailValue(int mode)
        {
            string strValue;
            switch (mode)
            {
                case 1:
                    strValue = System.Configuration.ConfigurationManager.AppSettings["FromMail"];
                    break;

                case 2:
                    strValue = System.Configuration.ConfigurationManager.AppSettings["FromServer"];
                    break;

                case 3:
                    strValue = System.Configuration.ConfigurationManager.AppSettings["FromName"];
                    break;

                case 4:
                    strValue = System.Configuration.ConfigurationManager.AppSettings["ReplyMail"];
                    break;

                case 5:
                    strValue = System.Configuration.ConfigurationManager.AppSettings["ReplyName"];
                    break;

                case 6:
                    strValue = System.Configuration.ConfigurationManager.AppSettings["MailBody"];
                    break;
                default:
                    strValue = "";
                    break;
            }


            return strValue;

        }

    }
    
}
